package com.psjj.privilege.service;

import com.psjj.privilege.dao.PrivilegeDao;
import com.psjj.privilege.dao.RoleDao;
import com.psjj.privilege.dao.UserDao;
import com.psjj.privilege.po.Privilege;
import com.psjj.privilege.po.Role;
import com.psjj.privilege.po.User;
import com.psjj.privilege.utils.MD5Util;

import java.sql.SQLException;
import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();
    private RoleDao roleDao = new RoleDao();
    private PrivilegeDao privilegeDao = new PrivilegeDao();

    public User login(String username, String pwd) {
        pwd = MD5Util.getMD5Str(pwd);
        User user = null;
        try {
            user = userDao.queryUserByNameAndPwd(username,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    public User loginJoinRoles(String username, String pwd) {
        pwd = MD5Util.getMD5Str(pwd);
        User user = null;
        try {
            user = userDao.queryUserByNameAndPwd(username,pwd);
            //关联用户对应的角色信息
            if(user!=null){
                int uid = user.getUid();
                //根据uid查询角色信息
                List<Role> roles = roleDao.queryRolesByUid(uid);
                user.setRoles(roles);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    public User loginJoinRolesJoinPrivileges(String username, String pwd) {
        pwd = MD5Util.getMD5Str(pwd);
        User user = null;
        try {
            user = userDao.queryUserByNameAndPwd(username,pwd);
            //关联用户对应的角色信息
            if(user!=null){
                int uid = user.getUid();
                //根据uid查询角色信息
                List<Role> roles = roleDao.queryRolesByUid(uid);
                //遍历遍历角色信息关联权限信息
                for(Role role : roles){
                    //获得roleid
                    int roleId  = role.getRid();
                    //根据roleid查询权限信息
                    List<Privilege> privileges = privilegeDao.queryPrivilegesByRoleId(roleId);
                    role.setPrivileges(privileges);
                }
                //设置角色信息
                user.setRoles(roles);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public int getCount(String username) {
        int count = 0;
        try {
            count = userDao.getCount(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<User> getPage(String username, int begin, int pageSize) {
        List<User> list = null;
        try {
            //查询所有用户数据
            list = userDao.getPage(username,begin,pageSize);
            //遍历用户数据关联角色数据
            list.forEach(user->{
                //获取当前的用户id
                int uid = user.getUid();
                //根据用户id查询角色信息
                List<Role> roles = null;
                try {
                    roles = roleDao.queryRolesByUid(uid);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                //给当前用户设置查询出的角色信息
                user.setRoles(roles);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void addUserJoinRoles(User user, String[] rids) {
        try {
            //保存用户数据并返回自增的uid值
            user.setPwd(MD5Util.getMD5Str(user.getPwd()));
            int uid = userDao.add(user);
            //将用户id和角色id维护到数据库中
            if(rids!=null && rids.length>0){
                for(String rid: rids){
                    userDao.addUidAndRid(uid,Integer.parseInt(rid));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public User queryUserByUid(int uid) {
        User user = null;
        try {
            user = userDao.queryUserByUid(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public void editUserJoinRole(User user, String[] rids) {
        try {
            //修改用户数据
            userDao.editUser(user);
            //清除之前用户与角色的关联信息
            userDao.clearUserIdJoinRoleIdByUserId(user.getUid());
            //修改用户和角色的关联数据
            userJoinRoleByUidAndRid(user,rids);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    private void userJoinRoleByUidAndRid(User user,String[] rids){
        try {
            if(rids!=null && rids.length>0){
                for(String rid : rids){
                    userDao.addUidAndRid(user.getUid(),Integer.parseInt(rid));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void del(int uid) {
        try {
            User user = userDao.queryUserByUid(uid);
            int status = 0;
            if(user.getStatus()==1){
                status = 0;
            } else {
                status = 1;
            }
            userDao.updateUserStatusByUid(uid,status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void resetPwd(int uid) {
        try {
            userDao.updatePwdByUid(uid,MD5Util.getMD5Str("123456"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void updatePassword(String username, String newPwd) {
        try {
            userDao.updatePwdByUsername(username,MD5Util.getMD5Str(newPwd));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
